package com.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bean.Admin;
import com.service.AdminService;
import com.service.impl.AdminServiceImpl;

@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		AdminService as = new AdminServiceImpl();
		String name = req.getParameter("adimName");
		String pass = req.getParameter("password");
		
		Admin admin = new Admin();
		admin.setAdminName(name);
		admin.setPassword(pass);
		
		Map<String,Object> result = as.adminLogin(admin);
		
		if(result.get("loginAdmin")!=null) {
			HttpSession session = req.getSession();
			session.setAttribute("loginAdmin", result.get("loginAdmin"));
			resp.sendRedirect("getGoods.do");
		}else {
			req.setAttribute("msg", result.get("msg"));
			req.getRequestDispatcher("login.jsp").forward(req, resp);
		}
		
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
